package fit.deni.components;

import java.util.Collection;

import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Import;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.corelib.components.EventLink;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.hibernate.Session;

import fit.deni.beans.Zanr;
import fit.deni.beans.Zaposleni;
import fit.deni.hibernate.interfaces.DVDService;
import fit.deni.hibernate.interfaces.LogService;
import fit.deni.hibernate.interfaces.ZanrService;
import fit.deni.pages.Genres;
import fit.deni.pages.Login;
import fit.deni.pages.Movies;
import fit.deni.utils.Utils;

@Import(stylesheet = "context:layout/css/navigation.css")
public class AdminNavigation {

	public AdminNavigation() {
		super();
		zanrovi = zanrService.getAll();
	}

	@InjectPage
	private Movies movies;

	@Component(parameters = { "event=logout" })
	private EventLink logout;
	@Component(parameters = { "event=showDVDs" })
	private EventLink sviDVD, noviDVD, popularniDVD;
	@Component(parameters = { "event=showGenres" })
	private EventLink sviZanrovi;
	@Component(parameters = {"event=selectGenre"})
	private EventLink selectedGenre;

	@Inject
	private ZanrService zanrService;
	@Inject
	private LogService logService;
	@Inject
	private DVDService dvdService;
	@Inject
	private Session session;
	@Property
	@SessionState
	private Zaposleni korisnik;

	private Object onLogout() {
		logService.addLog(Utils.createLog("User " + korisnik.getId() + ": " + korisnik.getUsername() + "  logged out"));
		korisnik = null;
		session.clear();
		reloadMenu();
		return Login.class;
	}

	private Object onShowDVDs(String param) {
		if (param.equals("svi")) {
			movies.setSelectedDVDs(dvdService.getAll());
			return movies;
		}
		if (param.equals("novi")) {
			movies.setSelectedDVDs(dvdService.getNewDVDs());
			return movies;
		}
		if (param.equals("popularni")) {
			movies.setSelectedDVDs(dvdService.getPopularDVDs());
			return movies;
		}
		return null;
	}

	private Object onShowGenres() {
		reloadMenu();
		return Genres.class;
	}
	
	private Object onSelectGenre(String zanr) {
		movies.setSelectedDVDs(dvdService.getDVDByGenre(zanr));
		reloadMenu();
		return movies;
	}

	@Property
	private Collection<Zanr> zanrovi;
	@Property
	private Zanr singleZanr;
	
	public void reloadMenu() {
		zanrovi = zanrService.getAll();
	}

}
